%load alt_calib/eq_mit_mass_shock.mat
addpath /mcr/res-m1wlg01/CEtools64/   
clear
%load ../parameter_sandbox_wage_rule/eq_mit_mass.mat 
load ../free_entry_labor/eq_mit_tfp.mat
%load ../prod/calibration_v3/sandbox/eq_mit_mass



set(0,'defaultaxesfontname','cambria math') % beautify the axes a bit
set(0,'defaultTextFontName', 'cambria math')

Ns = 1 - 6/100;

Labor       =  eq_mit.l;
 total_labor =  sum(eq_mit.mu.*Labor);

adj = total_labor/total_labor(end);
scx = pick_scale(adj, Ns);

T = 30;

figure(47)

subplot(3,3,1)
hold on


 costs = eq_mit.l;
 prods = repmat(exp(glob.sf(:,2)), 1, options.T);
 prods = prods.*repmat(eq_mit.A', glob.Nsf, 1);

 wages = repmat(eq_mit.W', glob.Nsf, 1);
 mkps  = eq_mit.p./(wages./prods);
 mu = mkps;

 tc    = sum(wages.*costs.*eq_mit.mu);
 num    = sum(wages.*costs.*eq_mit.mu.*mkps);

 mu_cw = num./tc;


Ys    = repmat(eq_mit.C',glob.Nsf,1);
Z     = sum(eq_mit.y./Ys./prods.*eq_mit.mu);
Z     = Z.^(-1);


%% plot real output, labor demand, and nominal gdp
close all

mass = log_linear_scale_plot(sum(eq_mit.mu,1), scx);

T = 15;
subplot(2,3,1)
plot(mass, 'LineWidth', 4); xlim([1 T]) ;title('Mass of estab.')
yline(0, '--')
ytickformat('percentage')

subplot(2,3,5)
plot(log_linear_scale_plot(eq_mit.C, scx), 'LineWidth', 4); xlim([1 T]); ylim(100*([.9 1]-1)); title('Output')
ytickformat('percentage')

subplot(2,3,6)
plot(log_linear_scale_plot(eq_mit.W, scx), 'LineWidth', 4); xlim([1 T]); ylim(100*([.95 1] - 1)); title('Wage')
ytickformat('percentage')

subplot(2,3,4)
plot(log_linear_scale_plot(total_labor, scx), 'LineWidth', 4); ylim(100*([.9 1]-1)); xlim([1 T]); title('Employment')
ytickformat('percentage')

ngdp = sum(eq_mit.mu.*eq_mit.p.*eq_mit.y);

subplot(2,3,2)
labor_bill = eq_mit.W'.*sum(eq_mit.l.*eq_mit.mu);
labor_share = labor_bill./ngdp;

 plot(log_linear_scale_plot(1./labor_share, scx), 'LineWidth', 4); xlim([1 T]); title('Markup')
 yline(0, '--')

ytickformat('percentage')

subplot(2,3,3)
tfp = eq_mit.C'./total_labor;
%tfp = tfp(1:T);
plot(log_linear_scale_plot(tfp, scx), 'LineWidth', 4); xlim([1 T]);
title('Effective TFP')
ytickformat('percentage')


set(gcf,'units','points','position',[10,10,1000,600])
set(findall(gcf,'-property','FontSize'),'FontSize',16)

print('-dpng', 'figures/some_irfs_tfp_shock.png')





%% decompose difference into change in dist vs change in policy
% 
costs = eq_mit.l.*wages;

% total costs are tc

mkp_ss = mkps(:,end);
mu_ss  = eq_mit.mu(:,end);
cost_ss = costs(:,end);
T= 30
for i = 1:T
    
    mu   = eq_mit.mu(:,i);
    mkp  = mkps(:,i);
    cost = costs(:,i);
    
    
    mkp_cw(i) = sum(mkp.*mu.*cost)/sum(mu.*cost);
    
    mkp_cw_cd(i) = sum(mkp.*mu_ss.*cost_ss)/sum(mu_ss.*cost_ss);
    
    mkp_cw_cp(i) = sum(mkp_ss.*mu.*cost)/sum(mu.*cost);

    
end

%%
mkp_cw = log_linear_scale_plot(mkp_cw, scx);
mkp_cw_cd = log_linear_scale_plot(mkp_cw_cd, scx);
mkp_cw_cp = log_linear_scale_plot(mkp_cw_cp, scx);

close all
plot(mkp_cw, 'LineWidth', 4)
hold on
plot(mkp_cw_cd, 'r--', 'LineWidth', 4)
%plot(mkp_cw_cp, 'LineWidth', 4)

xlim([1 15]);

legend('Cost Weighted Markup', 'Fixed Dist')

set(findall(gcf,'-property','FontSize'),'FontSize',12)
set(gcf,'units','points','position',[10,10,500,300])
ytickformat('percentage')

print('-dpng', 'figures/entry_markup_distribution_effect.png')

